<template>
  <div class="gpuinfo-page">
    <van-swipe class="product-swipe" :autoplay="3000" indicator-color="#60D978">
      <van-swipe-item>
        <img
          :src="productInfo.logo"
          :alt="productInfo.name"
        />
      </van-swipe-item>
    </van-swipe>

    <div class="product-info">
      <div class="info-card">
        <div class="info-row">
          <div class="info-label">{{ t("gpuinfo_price") }}</div>
          <div class="info-value">COP{{ formatNumber(productInfo.price || '0') }}</div>
        </div>
        <div class="info-row">
          <div class="info-label">{{ t("gpuinfo_profitRate") }}</div>
          <div class="info-value">{{ formatNumber(getTotalEarningsRate()) }}%</div>
        </div>
      </div>

      <div class="parameter-section">
        <div class="section-title">{{ t("gpuinfo_parameter") }}</div>
        <div class="parameter-list">
          <!-- <div class="parameter-item">
            <div class="item-label">
              Pendapatan dipengaruhi oleh volume produksi dan<span @click="goProduct"
                >tingkat hasil yang baik</span
              >
            </div>
          </div> -->
          <!-- <div class="parameter-item">
            <div class="item-label">Tingkat hasil yang baik</div>
            <div class="item-value">65-85%</div>
          </div> -->
          <!-- <div class="parameter-item">
            <div class="item-label">{{ t("gpuinfo_totalProfit") }}</div>
            <div class="item-value">COP{{ formatNumber(getDailyEarnings()) }} </div>
          </div> -->
          <div class="parameter-item">
            <div class="item-label">{{ t("gpuinfo_totalProfit") }}</div>
            <div class="item-value">COP{{ formatNumber(getTotalEarnings()) }}</div>
          </div>
          <div class="parameter-item">
            <div class="item-label">{{ t("gpuinfo_limit") }}</div>
            <div class="item-value">{{ productInfo.limitnum || '0' }}</div>
          </div>
          <div class="parameter-item">
            <div class="item-label">{{ t("products_validPeriod") }}</div>
            <div class="item-value">{{ productInfo.cycle || '0' }}</div>
          </div>
          <div class="parameter-item">
            <div class="item-label">{{ t("gpuinfo_vipCondition") }}</div>
            <div class="item-value">{{ t("gpuinfo_vipLevel", {w:productInfo.lever || '1'}) }}</div>
          </div>
        </div>
      </div>
    </div>

    <!-- <div class="contract-section">
      <van-checkbox v-model="agreeContract" shape="square" icon-size="16px">
        {{ t("gpuinfo_agreeContract") }}
        <span class="contract-link" @click="viewContract">({{ t("gpuinfo_viewContract") }})</span>
      </van-checkbox>
    </div> -->

    <div class="bottom-bar">
      <div class="price-container">
        <div class="price">{{ formatNumber(productInfo.price || '0') }}</div>
        <div class="label">{{ t("gpuinfo_price") }}</div>
      </div>
      <div class="invest-btn" @click="handleInvest">{{ t("products_investBtn") }}</div>
    </div>
    
    <!-- 加载状态 -->
    <div class="loading-container" v-if="loading">
      <van-loading type="spinner" color="#f6eade" size="28px" />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import { showToast } from "vant";
import { useRouter, useRoute } from "vue-router";
import { getLockMallInfo } from "@/api/products";
import { formatNumber } from "@/utils/public";

const {t} = useI18n()
const router = useRouter();
const route = useRoute();
const agreeContract = ref(true);
const loading = ref(false);
const productInfo = ref<any>({});

// 获取产品详情
const getProductDetail = async () => {
  if (!route.query.id) {
    showToast(t('gpuinfo_noId'));
    router.back();
    return;
  }
  
  try {
    loading.value = true;
    const res = await getLockMallInfo({
      id: route.query.id as string
    });
    
    if (res.code === 200 && res.data) {
      productInfo.value = res.data;
      route.meta.title = res.data.name
    } else {
      showToast(res.msg || t('gpuinfo_getDetailFail'));
    }
  } catch (error) {
    console.error('err', error);
    showToast(t('products_networkError'));
  } finally {
    loading.value = false;
  }
};

// 计算每日收益范围
const getDailyEarnings = () => {
  if (!productInfo.value) return '0-0';
  
  const price = parseFloat(productInfo.value.price || 0);
  const dayIncomeLow = parseFloat(productInfo.value.dayIncomeLow || 0);
  const dayIncomeHigh = parseFloat(productInfo.value.dayIncomeHigh || 0);
  
  const lowEarning = Math.round(price * dayIncomeLow);
  const highEarning = Math.round(price * dayIncomeHigh);
  
  return `${lowEarning}-${highEarning}`;
};

// 计算总收益范围
const getTotalEarnings = () => {
  if (!productInfo.value) return '0-0';
  
  const price = parseFloat(productInfo.value.price || 0);
  const cycle = parseFloat(productInfo.value.cycle || 0);
  const dayIncomeHigh = parseFloat(productInfo.value.dayIncomeHigh || 0);
  

  const highEarning = Math.round(price * dayIncomeHigh * cycle);
  
  return `${highEarning}`;
};

// 计算总收益率
const getTotalEarningsRate = () => {
  if (!productInfo.value) return '0-0';
  
  const cycle = parseFloat(productInfo.value.cycle || 0);
  const dayIncomeHigh = parseFloat(productInfo.value.dayIncomeHigh || 0);
  
  const highRate = Math.round(dayIncomeHigh * cycle * 100);
  
  return `${highRate}`;
};

// 投资处理
const handleInvest = () => {
  if (!agreeContract.value) {
    showToast(t('gpuinfo_agreeFirst'));
    return;
  }
  
  // 跳转到结算页面并传递产品信息
  router.push({
    path: "/static/settle",
    query: {
      id: route.query.id,
      name: productInfo.value.name,
      price: productInfo.value.price,
      logo: productInfo.value.logo,
      cycle: productInfo.value.cycle
    }
  });
};

// 查看合同
const viewContract = () => {
  router.push("/contract?content=" + encodeURIComponent(productInfo.value.info || ''));
};

// 跳转到新闻详情
const goProduct = () => {
  router.push("/newsdetail");
};

// 页面加载时获取产品详情
onMounted(() => {
  getProductDetail();
});
</script>

<style scoped>
.gpuinfo-page {
  min-height: 100vh;
  overflow: hidden;
  padding: 5px 0 0 0;
  color: #f6eade;
  background: url("@/assets/images/login/topBg.png") no-repeat top;
  background-size: 100% auto;
  position: relative;
}

.loading-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.product-swipe {
  width: calc(100% - 34px);
  overflow: hidden;
  margin: 0px 17px 0 17px;
  border-radius: 8px;
}

.product-swipe img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.info-card {
  width: calc(100% - 34px);
  overflow: hidden;
  padding: 22px 17px;
  margin: 17px;
  background: linear-gradient(121deg, #3f2b22 21%, #181414 67%);
  border: 1px solid hsla(0, 0%, 100%, 0.25);
  border-radius: 13px;
  display: flex;
  justify-content: space-between;
}

.info-row {
  width: 50%;
  display: flex;
  flex-direction: column;
}

.info-row:last-child {
  margin-bottom: 0;
  text-align: left;
  padding-left: 17px;
}

.info-label {
  font-size: 14px;
  padding: 0 0 15px 0;
  color: hsla(0, 0%, 100%, 0.6);
}

.info-value {
  font-size: 14px;
  color: #f6eade;
  padding: 0;
}

.parameter-section {
  padding: 0px 17px 0px 17px;
}

.section-title {
  width: 100%;
  overflow: hidden;
  font-size: 20px;
  margin-bottom: 11px;
  font-weight: 700;
}

.parameter-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 17px;
}

/*  .parameter-item:first-child {
  border: 0.5px solid rgba(153, 153, 153, 0.4);
  border-radius: 5px;
  padding: 5px;
  margin-bottom: 9px;
  display: flex;
  font-size: 12px;
}
.parameter-item:first-child .item-label {
  color: rgb(255, 162, 95);
  font-size: 12px;
  line-height: 1.4;
}
.parameter-item:first-child span {
  color: rgb(54, 96, 146);
  margin-left: 3px;
}
.parameter-item:last-child {
  margin-bottom: 0;
  padding-bottom: 0;
  border-bottom: none;
}
 */
.parameter-list {
  overflow: hidden;
  padding: 17px;
  background: linear-gradient(
    180deg,
    rgba(254, 242, 230, 0.01),
    rgba(254, 237, 221, 0.1) 62%
  );
  border-radius: 18px;
}

.item-label {
  color: hsla(0, 0%, 100%, 0.6);
  font-size: 13px;
  flex: 1;
  padding-right: 12px;
}

.item-value {
  font-size: 13px;
}

.contract-section {
  margin-top: 17px;
  display: flex;
  justify-content: start;
  width: 100%;
  padding-left: 22px;
}

:deep(.van-checkbox__label) {
  color: rgb(246, 234, 222);
  font-size: 16px;
  line-height: 16px;
}

.contract-link {
  margin-left: 11px;
  color: rgb(54, 96, 146);
}

:deep(.van-checkbox__icon) {
  background-color: rgb(255, 255, 255);
  border-color: rgba(246, 234, 222, 0.4);
  border-radius: 3px;
}

:deep(.van-checkbox__icon--checked .van-icon) {
  color: #000 !important;
  background-color: rgb(246, 224, 202);
  border-color: rgb(246, 224, 202);
}

.bottom-bar {
  height: 74px;
  overflow: hidden;
  padding: 17px 0 0 22px;
  border-top: 1px solid #635b59;
  border-radius: 13px 13px 0 0;
  background-color: #211a18;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  position: fixed;
  z-index: 999;
  bottom: 0px;
  left: 0px;
  width: 100%;
}

.price-container {
  display: flex;
  flex-direction: column;
}

.price {
  color: rgb(255, 162, 95);
  font-size: 20px;
  font-weight: bold;
}

.label {
  color: hsla(0, 0%, 100%, 0.6);
  font-size: 12px;
  margin-top: 4px;
}

.invest-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  width: 194px;
  height: 45px;
  overflow: hidden;
  font-size: 20px;
  color: #0c1f26;
  background: url("../../assets/images/btnBg1.png") no-repeat;
  background-size: 100% 100%;
}

:deep(.van-button--primary) {
  background: linear-gradient(
    180deg,
    rgb(254, 242, 230) 0%,
    rgb(246, 224, 202) 100%
  );
  border: none;
  color: rgb(33, 26, 24);
}
</style>
